(function (){
	IS.init( $('#uploadImg'), $('.zhaopiantxt'), 'uploadImg' );
	$('.sendbtn').click(function(){
		MSG.checkPostData();
	});
	$( '#msgTxt' ).keypress( function ( event ){
		UI.isCtrlEnter( event, function (){
			MSG.checkPostData();
		});
	});
	$('#newTopic').click(function(){
		MSG.txtTopic = '#输入话题标题#';
		MSG.addTopic();
	});
	$('#msgTxt').keyup(function(){
		MSG.countTxt();
	});
	//微博输入框发送按钮
	$( '#sendbtn' ).mouseover(function(){
		$(this).css( { 'background-position' : '0px -31px'} );
	}).mouseout(function(){
		$(this).css( { 'background-position' : '0px 0px'} );
	});
})();

$(function(){
	//朋友账号
	$('body').append('<div style="top: 308px; left: 428px;" class="atWrap" id="atWrap"><div style="" class="musicTab"><div class="atSearch"><input type="text" class="inputTxt"><span class="atbtn"><input type="button" class="btn_search2"><a title="清空" class="del" href="#"></a></span></div><a title="关闭" class="close" href="#"></a></div><div style="height: 220px; overflow-y: scroll; overflow-x: hidden;" class="autoCmt"><div class="autoCmtAll" style="display: block;"><ul>  正在读取数据, 请稍等^^  <img src="style/images/loading_16.gif" width="16px" height="16px"/></ul></div><div class="autoCmtKey" style="display: none;"><ul></ul></div></div><div style="" class="tips">@朋友帐号,他就能在[提到我的]页收到</div></div>');
	//重定位好友列表
	

	//显示好友列表
	$('#pengyoutxt').click(function(){
			var pyoffset = $('.pengyoutxt').offset();
				$('#atWrap').css({top:pyoffset.top+20+'px',left:pyoffset.left-19+'px'});
			if(!$('.autoCmtAll > ul > li').length)
			{
				$.get('index.php', {m:"a_myidollist"},function(data){
					try{
					data = eval('('+data+')');
					if(data.code == 0)
					{
						dlength = data.data.length;
						myIdollistStr = '';
						for(i in data.data)
						{
								if(i>0)
								{
									myIdollistStr = '<li><span>'+ data.data[i].nick+' ('+data.data[i].name+')</span></li>'
									$('.autoCmtAll > ul').append( myIdollistStr )
								}else{
									myIdollistStr ='<ul><li class="on"><span>'+ data.data[i].nick+' ('+data.data[i].name+')</span></li></ul>';
									$('.autoCmtAll').html( myIdollistStr )
								}

						}
					}
					}catch(e){
						alert(e)
					}
					//li点击事件
					$('.autoCmtAll > ul > li, .autoCmtKey > ul > li').click(function(){
						str = $(this).html();
						start = str.lastIndexOf('(')
						end = str.lastIndexOf(')')
						if(start && end)
						{
							uid = str.substring(start+1,end);
							MSG.insertTxt(MSG._txt,'@'+uid+' ')
							MSG._txt.focus();
							MSG.countTxt();
						}else{
							alert('出错了');
						}
					})

				})//get 完
			}

		if($('#atWrap').css('display')=='none'){
			$('#atWrap').show()
		}else{
				$('#atWrap').hide() ;
		}
	})

	//li选中状态
	$('.autoCmtAll > ul > li, .autoCmtKey > ul > li').live('mouseover',function(){
		$('.autoCmtAll > ul > li').add('.autoCmtKey > ul > li').removeClass('on')
		$(this).addClass('on');
	})

	//关闭好友列表
	$('.close').click(function(){
			$('#atWrap').hide();
	})
	//点击其他地方 关闭好友列表
	document.onclick = function(e)
	{
		 $('#atWrap').hide();
	}
	$(".pengyoutxt, #atWrap").click(function(e) {  
		e.stopPropagation();
	}); 

	//输入框更改值事件
	var inputTxtValue='';
	$('.inputTxt').live('keyup',function(){
		if($('.inputTxt').val().trim() != inputTxtValue)
		{
			inputTxtValue = $('.inputTxt').val().trim()
			if(inputTxtValue.length)
			{
				//有输入过滤好友
				$('.autoCmtKey > ul').html('')
				$('.autoCmtAll > ul > li').each(function(){
					if($(this).html().indexOf(inputTxtValue)>=0)
					{
						$('.autoCmtKey > ul').append('<li>'+$(this).html()+'</li>')
					}
				})

				$('.autoCmtKey').show()
				$('.autoCmtAll').hide()

					//li点击事件
					$('.autoCmtAll > ul > li, .autoCmtKey > ul > li').click(function(){
						str = $(this).html();
						start = str.lastIndexOf('(')
						end = str.lastIndexOf(')')
						if(start && end)
						{
							uid = str.substring(start+1,end);
							MSG.insertTxt(MSG._txt,'@'+uid+' ')
							MSG._txt.focus();
							MSG.countTxt();
						}else{
							alert('出错了');
						}
					})
			}else{
				//无输入显示原始数据
				$('.autoCmtAll').show()
				$('.autoCmtKey').hide()
			}
		}
	})
})
		
$(function(){
	var emotionsbox=$(['<div class=\"atWrap emotions\" id=\"emotionsWrap\">',
					 '<a title="关闭" class="close" href="javascript:void(0);"></a>',
					 '<div id=\"emotionbox\"></div>',
					 '<span class="angle"></span>',	
					 '</div>'].join("")).appendTo($('body'));
	emotionsbox.find(".close").one("click",function(){
	emotionsbox.hide();	
	});
	function getemotion($1)
	{	var s=$1.replace(",","").split("|");
		return "<a href=\"javascript:void(0)\" class=\""+s[0]+"\" title=\""+s[1]+"\"></a>";	
	}
	$("#emotionbox").html(emotions.replace(/(f\d+\|[^,]+),?/g,getemotion)).append($("<div><p></p><h3></h3></div>"));
	$("#biaoqingtxt").click(function(){
		var eboffset = $(this).offset();
		emotionsbox.css({top:eboffset.top+25+'px',left:eboffset.left-19+'px'}).show();	
		});
	$("#emotionbox").hover(function(){$(this).find("div").show();});
		$("#emotionbox a").hover(function(){
			var o=$(this);var x=o.offset().left-o.parent().offset().left-o.parent().width()/2;
			var op=$("#emotionbox").find("div");
			if(x>0){op.css("left",0);op.css("right","auto")}else{op.css("right",0);op.css("left","auto")}
			op.find("p").html("<img src=\"style/images/emotions/"+o.attr("class").replace("f","")+".gif\"/>");
			op.find("h3").html(o.attr("title"));
		}).click(function(){var t=$(this);
		//MSG.insertTxt(MSG._txt,"\\"+t.attr("title"))
		MSG._txt.value=MSG._txt.value+"/"+$(this).attr("title");
		MSG._txt.focus();
		MSG.countTxt();
		emotionsbox.hide();
	});
});
		
$(function(){
	function showMedia(vt)
	{//vt：媒体类型 0:视频 1：音乐
		var va=["",];
		var str="";
		//Mozilla/5.0 (Windows NT 6.1; rv:2.0) Gecko/20100101 Firefox/4.0
		if(navigator.userAgent.toLowerCase().indexOf("firefox")>=0)
		{style1+="height:20px;";}
	  if(vt==0)
	  {str=[
	  	  	"<p style=\"margin:12px 8px 0\">粘贴视频播放页地址<font color=\"#999\">(优酷、土豆、56等网站视频可直接播放)</font></p>",
	  	  	"<p style=\"margin:7px 8px 15px;\">",
	  	  	"<input type=\"text\" value=\"\" class=\"text\" style=\"width:290px;\"/>",
	  	  	"<input type=\"button\" value=\"确定\" class=\"button\"/></p>",
	  	  	"<a title=\"关闭\" class=\"close\" href=\"javascript:void(0);\"></a>",
	  	  	"<p style=\"display:none;margin:-7px 0 15px 12px;padding:0;color:#f60;\" id=\"videonosupport\">暂不支持该视频地址，<a href=\"javascript:void(0);\">作为普通链接显示</a></p>",
	  	  	"<span class=\"angle\"></span>"	
		   ].join("\n");}
		else if(vt==1)
		{str=[
				"<p style=\"margin:12px 8px 0\"><span style=\"width:16px;height:16px;background:url(style/images/btns.gif) -115px -510px no-repeat;display:inline-block;vertical-align:middle;margin-right:8px;\"></span>输入歌名/歌手名，点击搜索找歌</font></p>",
				"<p style=\"margin:7px 8px 15px;\">",
				"<input type=\"text\" value=\"tx\" class=\"text\" style=\"width:290px;\" placeholder=\"输入歌名/歌手名，点击搜索找歌\"/>",
				"<input type=\"button\" value=\"搜索\" class=\"button\"/></p>",
				"<a title=\"关闭\" class=\"close\" href=\"javascript:void(0);\"></a>",
				"<p style=\"margin:-7px 0 15px;padding:0;color:#999;width:100%;display:none;\" class=\"musicinfo\"></p>",
				"<p class=\"player\"></p>",
				"<p style=\"display:none;margin:-7px 0 15px 12px;padding:0;color:#f60;\" id=\"musicnosupport\"></p>",
				"<span class=\"angle\"><audio id=\"html5media\" height=\"0\" width=\"0\" autoplay=\"false\" src=\"http://stream16.qqmusic.qq.com/30738969.mp3\" state=\"3\"><span id=\"mediaplayer\"><embed name=\"wmplayer\" id=\"wmplayer\" type=\"application/x-mplayer2\"    pluginspage=\"http://www.microsoft.com/isapi/redir.dll?prd=windows&sbp=mediaplayer&ar=media&sba=plugin&\" wmode=\"opaque\" autostart=\"1\" src=\"about:blank\" width=\"0\" height=\"0\"/></span></audio></span>"	
			].join("\n");}
		return str;
	}
	
	var mediabox=$('body').append('<div class=\"atWrap\" id=\"mediaWrap\" style=\"width:370px;\"><script type=\"text/javascript\" src=\"http://qzone-music.qq.com/fcg-bin/fcg_set_musiccookie.fcg?fromtag=6&p=' + Math.random()+'\"></script></div>');
	$('#yinyuetxt').click(function(){
	if(/<font[\s\S]*?>[\s\S]*<\/font>/g.test($(this).html())){return false;}
	var mboffset = $('#yinyuetxt').offset(),timer;
	var f=$('#mediaWrap'),ft,fb;	
		f.css({top:mboffset.top+25+'px',left:mboffset.left-19+'px'});
		f.html(showMedia(1)).show();
		ft=f.find("input[type='text']").first();
		fb=f.find("input[type='button']").first();
		player=f.find(".player");
		emsg=$("#musicnosupport");
		minfo=f.find(".musicinfo");
		f.find(".close").one('click',function(){f.hide();});
		fb.click(function(){
			searchMusicList(1);
			clearInterval(timer);
		});		
	  
	  	function searchMusicList(_p)
		{
			  $.get("?m=getmusicinfo",{"w":ft.val(),"p":_p},function(data){
			  function searchJsonCallback(o)
			  {if(o.retcode==0&&o.totalnum>0)
			  	 {	
			  	 	 var list=o.list,str="",pageCount=Math.ceil(o.totalnum/5);
			  	 	 function getRealMusicSrc(str)
			  	 	 {	var _url=function($1){return "30"+$1.slice(2);}
			  	 	 		str=str.replace("stream","stream1").replace(/(\d+)\.wma/,_url).replace(".wma",".mp3"); 
			  	 	 return str;
			  	 	 }
			  	 	 
				  	 for(var i=0;i<list.length;i++)
				  	 {	list[i].songurl=getRealMusicSrc(list[i].songurl);
				  	 	str+="<li singername=\""+list[i].singername+"\" songname=\""+list[i].songname+"\" songurl=\""+list[i].songurl+"\"><span><a href=\"\">"+list[i].songname+"</a> - <font color=\"gray\">"+list[i].singername+"</font></span><a href=\"javascript:void(0)\" class=\"playbtn\"></a></li>";
				  	 }
				  	 str=["<p style=\"color:gray;margin:0 18px 8px;\">搜索结果("+o.totalnum+"首)</p>","<ul class=\"musiclist\">",str,"</ul>"].join("");
				  	 if(pageCount>1)
				  	 {var page=function($1)
				  	 	 {	  var i=parseInt($1,10);
				  	 	 	  var _str="<a href=\"javascript:void(0)\" value=\""+i+"\">"+i+"</a>";
				  	 	 	  if(_p==i){_str="<strong>"+i+"</strong>";}
				  	 	 	  if(i<1||i>pageCount){_str="";}
				  	 	 	  return _str;
				  	 	 }
				  	 		 
				  	 		pagearr=[_p-2,_p-1,_p,_p+1,_p+2].join(" ");
				  	 		pagearr=pagearr.replace(/(\S+)/g,page);
				  	 		str+=["<p style=\"margin:-10px 18px 0;font-size:14px;\" class=\"pageinfo\">",
				  	 			_p<=1?"":"<a href=\"javascript:void(0);\" value=\""+(_p-1)+"\">上一页</a> ",
				  	 			_p>3?page(1)+"...":"",
				  	 			pagearr,
				  	 			_p<pageCount-2?"..."+page(pageCount):"",
				  	 			_p>=pageCount?"":" <a href=\"javascript:void(0);\" value=\""+(_p+1)+"\">下一页</a>",
				  	 		"</p>"].join("");
				  	 	
				  	 }
				  	 minfo.html(str).show();
				  	 minfo.find(".pageinfo").find(" a").one('click',function(){searchMusicList(parseInt($(this).attr("value"),10))});
				  	 emsg.hide();
				  	 
				  	 minfo.find(".musiclist").find("a").click(function(){
				  	 	if($(this).hasClass("playbtn"))
				  	 	{var playerbtn=$(this),songurl=$(this).parent().attr("songurl"),html5audiosupport=false,player;
				  	 	 $("#html5media").attr("src",songurl);
				  	 	 try{$("#html5media")[0].pause();html5audiosupport=true;player=$("#html5media")[0];}
				  	 	 catch(e)
				  	 	 {html5audiosupport=false;try{$("#wmplayer")[0].pause();}catch(e){}
				  	 	  $("#mediaplayer").html("<embed name=\"wmplayer\" id=\"wmplayer\" type=\"application/x-mplayer2\"    pluginspage=\"http://www.microsoft.com/isapi/redir.dll?prd=windows&sbp=mediaplayer&ar=media&sba=plugin&\" wmode=\"opaque\" autostart=\"1\" src=\""+songurl+"\" width=\"0\" height=\"0\"/>");player=$("#wmplayer")[0];/*
				  	 	 	function onplayerStageChange()
				  	 	 	{
				  	 	 		//播放状态，1=停止，2=暂停，3=播放，6=正在缓冲，7=等待,8=播放完成,9=正在连接，10=准备就绪 
				  	 	 		if(player.PlayState!=1&&player.PlayState!=2&&player.PlayState!=3&&player.PlayState!=8)
				  	 	 		{playerbtn.addClass("loading");}
				  	 	 		else
				  	 	 		{playerbtn.removeClass("loading");}
				  	 	 	}
				  	 	 	 clearInterval(timer);
				  	 	 	 timer=setInterval(onplayerStageChange,1000);*/
				  	 	 }
				  	 	 if ($(this).hasClass("pausebtn"))
				  	 		{
				  	 		  minfo.find(".musiclist").find(".pausebtn").removeClass("pausebtn");
				  	 		  try
				  	 		  {player.pause();}
				  	 		  catch(e)
				  	 		  {
				  	 		  	player.controls.pause();
				  	 		  }
				  	 		}else
				  	 		{ 
				  	 		  minfo.find(".musiclist").find(".pausebtn").removeClass("pausebtn");
				  	 		  $(this).addClass("pausebtn");
				  	 		  try{player.play();}
				  	 		  catch(e)
				  	 		  {try{player.controls.play();}catch(e1){}} 	  	
				  	 		} 	
				  	 	}
				  	 	else
				  	 	{	var o=$(this).parent().parent();
				  	 		var music={"name":o.attr("songname"),"songurl":o.attr("songurl"),"singername":o.attr("singername")};
				  	 		$("#yinyuetxt").html("<font color=\"gray\">"+music.name+"</font>");
				  	 		$(".cancelMusic").css("display","inline-block").one("click",function(){$("#yinyuetxt").html("音乐");$(this).css("display","inline");
				  	 		$("#msgTxt").val($("#msgTxt").val().replace(" #音乐分享# "+music.name+" - "+music.singername,""));
				     	 	MSG.countTxt();
				     	 	$("#sendTweet").find("input[name='music']").val("");
				     	 	});
				  	 		$("#mediaWrap").hide();
				  	 		$("#sendTweet").find("input[name='music']")
				  	 		.val("{'url':'"+music.songurl+"','Title':'"+music.name+"','Author':'"+music.singername+"'}");
				  	 		//$("#msgTxt").val($("#msgTxt").val()+" #音乐分享# "+music.name+" - "+music.singername);
				  	 		MSG.insertTxt(MSG._txt," #音乐分享# "+music.name+" - "+music.singername)
				  	 		MSG.countTxt();					
				  	 	}
				  	 	return false;
				  	 });
				 }
			   else
			     {emsg.show().html("没有搜索到相关音乐！");}
			  }
			  try{eval(data)}catch(e){alert(e);};
			});	
		}
	});
	$('#shipintxt').click(function(){
		var mboffset = $('#shipintxt').offset();
		var f=$('#mediaWrap');
		f.css({top:mboffset.top+25+'px',left:mboffset.left-19+'px'});
		if($("#videoThumbImage").size()==0)
		{$("#mediaWrap").html(showMedia(0)).show();}
		else
		{return;}
		f.find(".close").click(function(){
		f.hide();
		});
		$('#mediaWrap').find("input[class='button']").click(function(){
			var ft=f.find("input[type='text']").first();
				if(/^\s*$/.test(ft.val())){return;}
				$.ajax({
				   type: "POST",
				   url: "?m=getvideoinfo",
				   data:{"url":ft.val()},
				   success: function(msg){msg=msg.replace(/&#160;/g,"");
				     try{
				     	 var d=eval('('+msg+')');
				     	 if(d.ret==0)
				     	 {
				     	 	var o=d.data;
				     	 	if(o.minipic=="")
				     	 	{
				     	 	$("#videonosupport").show()
				     	 						 .one("click",function(){
				     	 						 MSG.insertTxt(MSG._txt,o.title+" "+o.short);
				  	 							 MSG.countTxt();	
				     	 						 f.hide();
					     	 					 });
					     	 return;	
				     	 	}			     	 	
				     	 	var thumbimg=$("<div id=\"videoThumbImage\"><img src=\""+o.minipic+"\"/><div class=\"icon\"></div></div>").appendTo($("body")).hide();
				     	 	MSG.insertTxt(MSG._txt,"#分享视频# "+o.title+" "+o.short);
				  	 		MSG.countTxt();
				  	 		$("#sendTweet").find("input[name='video']").val(o.real);
				     	 	$("#shipintxt").html("<font color=\"gray\">"+o.title+"</font>").attr("title","").parent()
				     	 	.find(".cancelVideo").css("display","inline-block")
				     	 	.one('click',function(){
				     	 		$("#msgTxt").val($("#msgTxt").val().replace("#分享视频# "+o.title+" "+o.short,""));
				     	 		MSG.countTxt();
				     	 		$(this).hide();
				     	 		$("#sendTweet").find("input[name='video']").val("");
				     	 		$("#shipintxt").html("视频").attr("title","腾讯、土豆、优酷、PPTV可直接播放");
				     	 		thumbimg.remove();
				     	 	});
				     	 	$("#shipintxt").bind("mouseover",function(){
				     	 	thumbimg.css({top:(mboffset.top+20)+'px',left:(mboffset.left-19)+'px'}).show();
				     	 	}).bind("mouseout",function(){thumbimg.hide();});	 
				     	 f.hide();	
				     	 }else{alert(d.msg);}
				     }catch(e)
				     {alert(msg);}
				   }
				});
		});
	});
});